<?php
namespace app\gc\admin;
use app\admin\controller\Admin;

use app\gc\model\Gc_gs as m_table;

class Gcgs extends Admin
{
    public function index($q = '')
    {
        $map = [];
        if ($q) {
            // 用户名、昵称
			$map['name'] = ['like', '%'.$q.'%'];
        }
		
        $data_list = m_table::where($map)->order('orderby desc, id desc')->paginate();
		
        // 分页
        $pages = $data_list->render();
        $this->assign('data_list', $data_list);
        $this->assign('pages', $pages);
		 
        return $this->fetch();
    }
	
	public function add($id = 0)
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
			
            unset($data['id']);
			unset($data['file']);
            if (!m_table::create($data)) {
                return $this->error('添加失败！');
            }
            return $this->success('添加成功。');
        }
        return $this->fetch('form');
    }
	
    public function edit($id = 0)
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
           unset($data['file']);
            if (!m_table::update($data)) {
                return $this->error('修改失败！');
            }
            return $this->success('修改成功。');
        }

        $row = m_table::where('id', $id)->find()->toArray();
        $this->assign('data_info', $row);
        return $this->fetch('form');
    }
	
}